﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace SpriteMaker
{
    public partial class formMain : Form
    {
        private ModulWorkingPanel moduleWorkingPanel;
        private FrameWorkingPanel frameWorkingPanel;
        private AnimPlayerPanel animPlayerPanel;
        private BoundWorkingPanel boundWorkingPanel;
        private Image image;
        private Image imPause;
        private Image imPlay;
        private String spritePath;
        public formMain()
        {
            InitializeComponent();
            Sprite.spriteInstance = new Sprite();
        }

        private void formMain_Load(object sender, EventArgs e)
        {
            Text = Sprite.spriteInstance.spriteName;
            moduleWorkingPanel = new ModulWorkingPanel();
            splitContainerModule.Panel2.Controls.Add(moduleWorkingPanel);
            moduleWorkingPanel.formInstance = this;
            frameWorkingPanel = new FrameWorkingPanel();
            frameWorkingPanel.formInstance = this;
            splitContainerFrame.Panel2.Controls.Add(frameWorkingPanel);
            boundWorkingPanel = new BoundWorkingPanel();
            boundWorkingPanel.formInstance = this;
            splitContainerBounding.Panel2.Controls.Add(boundWorkingPanel);
            animPlayerPanel = new AnimPlayerPanel();
            animPlayerPanel.formInstance = this;
            animPlayerPanel.Location = new Point(0, toolStripAnimPlayer.Height);
            animPlayerPanel.Size = new System.Drawing.Size(splitContainerAnim.Panel2.ClientSize.Width, splitContainerAnim.Panel2.ClientSize.Height - toolStripAnimPlayer.Height);
            splitContainerAnim.Panel2.Controls.Add(animPlayerPanel);
            System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
            Stream myStream = myAssembly.GetManifestResourceStream("SpriteMaker.Pause.png");
            imPause = new Bitmap(myStream);
            myStream = myAssembly.GetManifestResourceStream("SpriteMaker.Play.png");
            imPlay = new Bitmap(myStream);
            Sprite.spriteInstance.formInstance = this;
        }

        public void ImportImage()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Image|*.png;*.tga;*.dds";
            dialog.ShowDialog();
            if (dialog.FileName != "")
            {
                LoadImage(dialog.FileName);
                Refresh();
            }
        }
        public void LoadImage(String path)
        {
            String extension = System.IO.Path.GetExtension(path);
            if (String.Compare(extension, ".dds", true) == 0)
            {
                byte[] data = File.ReadAllBytes(path);
                image = Util.DDSToBitmap(data);
                Sprite.spriteInstance.imageName = System.IO.Path.GetFileName(path);
            }
            else
            {
                image = Image.FromFile(path);
                Sprite.spriteInstance.imageName = System.IO.Path.GetFileName(path);
            }
        }
        public Image GetImage()
        {
            return image;
        }
        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
            object[] data = new object[dataGridViewModule.ColumnCount];
            data[0] = dataGridViewModule.Rows.Count;
            int id = Sprite.spriteInstance.AddModule();
            data[1] = id.ToString("X");
            data[2] = 0;
            data[3] = 0;
            data[4] = 20;
            data[5] = 20;
            data[6] = "";
            row.CreateCells(dataGridViewModule, data);
            dataGridViewModule.Rows.Add(row);
            dataGridViewModule.ClearSelection();
            row.Selected = true;
        }

        private void toolStripButtonRemove_Click(object sender, EventArgs e)
        {
            int count = dataGridViewModule.SelectedRows.Count;
            for (int i = 0; i < count; i++)
            {
                int id = Convert.ToInt32(dataGridViewModule.SelectedRows[0].Cells["ColumnID"].Value);
                Sprite.spriteInstance.RemoveModule(id);
                dataGridViewModule.Rows.RemoveAt(dataGridViewModule.SelectedRows[0].Index);
            }
            
        }

        private void dataGridViewModule_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridViewModule_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewModule.SelectedRows.Count == 0) return;
            int id = Convert.ToInt32(dataGridViewModule.SelectedRows[0].Cells["ColumnID"].Value);
            moduleWorkingPanel.SetActiveModule(Sprite.spriteInstance.GetModule(id));
        }

        private void dataGridViewModule_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            //int id = Convert.ToInt32(dataGridViewModule.Rows[e.RowIndex].Cells["ColumnID"].Value);
            //moduleWorkingPanel.SetActiveModule(sprite.GetModule(id));
        }
        public void UpdateActiveModule()
        {
            Module module = moduleWorkingPanel.GetActiveModule();
            if (dataGridViewModule.SelectedRows.Count == 1 
                && Convert.ToInt32(dataGridViewModule.SelectedRows[0].Cells["ColumnID"].Value) == module.id)
            {
                dataGridViewModule.EndEdit();
                dataGridViewModule.SelectedRows[0].Cells["ColumnX"].Value = module.x;
                dataGridViewModule.SelectedRows[0].Cells["ColumnY"].Value = module.y;
                dataGridViewModule.SelectedRows[0].Cells["ColumnWidth"].Value = module.width;
                dataGridViewModule.SelectedRows[0].Cells["ColumnHeight"].Value = module.height;
            }
        }

        private void dataGridViewModule_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            if (index < 0) return;
            int id = Convert.ToInt32(dataGridViewModule.Rows[index].Cells["ColumnID"].Value);
            int x = Convert.ToInt32(dataGridViewModule.Rows[index].Cells["ColumnX"].Value);
            int y = Convert.ToInt32(dataGridViewModule.Rows[index].Cells["ColumnY"].Value);
            int w = Convert.ToInt32(dataGridViewModule.Rows[index].Cells["ColumnWidth"].Value);
            int h = Convert.ToInt32(dataGridViewModule.Rows[index].Cells["ColumnHeight"].Value);
            String d = Convert.ToString(dataGridViewModule.Rows[index].Cells["ColumnDesc"].Value);
            try
            {
                Sprite.spriteInstance.SetModuleValue(moduleWorkingPanel.GetActiveModule().id, id, x, y, w, h, d);
            }
            catch (Exception ex)
            {
                Module module = moduleWorkingPanel.GetActiveModule();
                dataGridViewModule.SelectedRows[index].Cells["ColumnID"].Value = module.id;
                dataGridViewModule.SelectedRows[index].Cells["ColumnX"].Value = module.x;
                dataGridViewModule.SelectedRows[index].Cells["ColumnY"].Value = module.y;
                dataGridViewModule.SelectedRows[index].Cells["ColumnWidth"].Value = module.width;
                dataGridViewModule.SelectedRows[index].Cells["ColumnHeight"].Value = module.height;
                dataGridViewModule.SelectedRows[index].Cells["ColumnDesc"].Value = module.description;
            }
        }

        private void dataGridViewModule_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void dataGridViewModule_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.AddExtension = true;
            dialog.DefaultExt = "sprite";
            dialog.ShowDialog();
            if (dialog.FileName != "")
            {
                TextWriter writer = new StreamWriter(dialog.FileName, false, Encoding.ASCII);
                writer.Write(Sprite.spriteInstance.ToStringXML());
                writer.Close();
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Sprite|*.sprite";
            dialog.ShowDialog();
            if (dialog.FileName != "")
            {
                TextReader reader = new StreamReader(dialog.FileName, Encoding.ASCII);
                String data = reader.ReadToEnd();
                reader.Close();
                Sprite.spriteInstance.FromStringXML(data);
                spritePath = System.IO.Path.GetDirectoryName(dialog.FileName) +"\\";
                InitUIFromSprite(Sprite.spriteInstance);
            }
        }

        private void toolStripButtonAddFrame_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
            object[] data = new object[dataGridViewFrame.ColumnCount];
            data[0] = dataGridViewFrame.Rows.Count;
            int id = Sprite.spriteInstance.AddFrame();
            data[1] = id.ToString("X");
            data[2] = "";
            row.CreateCells(dataGridViewFrame, data);
            dataGridViewFrame.Rows.Add(row);
            dataGridViewFrame.ClearSelection();
            row.Selected = true;
        }

        private void toolStripButtonAddFrameModule_Click(object sender, EventArgs e)
        {
            if (Sprite.spriteInstance.frameList.Count() == 0 || dataGridViewFrame.SelectedRows.Count == 0)
            {
                MessageBox.Show("Please select a frame first");
                return;
            }
            if (Sprite.spriteInstance.moduleList.Count() == 0)
            {
                MessageBox.Show("Please add module first");
                return;
            }
            DataGridViewRow row = new DataGridViewRow();
            object[] data = new object[dataGridViewFrameModule.ColumnCount];
            data[0] = dataGridViewFrameModule.Rows.Count;
            int frameID = Convert.ToInt32(dataGridViewFrame.SelectedRows[0].Cells["ColumnFrameID"].Value);
            data[1] = Sprite.spriteInstance.AddFrameModule(frameID).ToString("X"); ;
            data[2] = 0;
            data[3] = 0;
            row.CreateCells(dataGridViewFrameModule, data);
            dataGridViewFrameModule.Rows.Add(row);
            dataGridViewFrameModule.ClearSelection();
            row.Selected = true;
            frameWorkingPanel.Refresh();
        }

        private void dataGridViewFrame_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewFrame.SelectedRows.Count == 0) return;
            int id = Convert.ToInt32(dataGridViewFrame.SelectedRows[0].Cells["ColumnFrameID"].Value);
            frameWorkingPanel.SetActiveFrame(Sprite.spriteInstance.GetFrame(id));
            dataGridViewFrameModule.Rows.Clear();
            foreach (FrameModule frameModule in frameWorkingPanel.GetActiveFrame().frameModuleList)
            {
                DataGridViewRow row = new DataGridViewRow();
                object[] data = new object[dataGridViewFrameModule.ColumnCount];
                data[0] = dataGridViewFrameModule.Rows.Count;
                data[1] = frameModule.module.id.ToString("X"); ;
                data[2] = frameModule.x;
                data[3] = frameModule.y;
                row.CreateCells(dataGridViewFrameModule, data);
                dataGridViewFrameModule.Rows.Add(row);
                //row.Selected = true;
            }
        }

        private void dataGridViewFrame_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            if (index < 0) return;
            int id = Convert.ToInt32(dataGridViewFrame.Rows[index].Cells["ColumnFrameID"].Value);
            String d = Convert.ToString(dataGridViewFrame.Rows[index].Cells["ColumnFrameDesc"].Value);
            Sprite.spriteInstance.SetFrameValue(frameWorkingPanel.GetActiveFrame().id, id, d);
        }

        private void dataGridViewFrameModule_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            if (index < 0) return;
            int id = Convert.ToInt32(dataGridViewFrameModule.Rows[index].Cells["ColumnFrameModuleID"].Value);
            int x = Convert.ToInt32(dataGridViewFrameModule.Rows[index].Cells["ColumnFrameModuleX"].Value);
            int y = Convert.ToInt32(dataGridViewFrameModule.Rows[index].Cells["ColumnFrameModuleY"].Value);
            Sprite.spriteInstance.SetFrameMoudleValue(frameWorkingPanel.GetActiveFrame().id, index, id, x, y);
            frameWorkingPanel.Refresh();
        }

        private void dataGridViewFrameModule_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewFrameModule.SelectedRows.Count == 0) return;
            int index = dataGridViewFrameModule.SelectedRows[0].Index;
            frameWorkingPanel.SetActiveFrameModule(frameWorkingPanel.GetActiveFrame().GetFrameModuleAt(index));
        }
        public void UpdateActiveFrameModule()
        {
            dataGridViewFrameModule.EndEdit();
            dataGridViewFrameModule.SelectedRows[0].Cells["ColumnFrameModuleX"].Value = frameWorkingPanel.GetActiveFrameModule().x;
            dataGridViewFrameModule.SelectedRows[0].Cells["ColumnFrameModuleY"].Value = frameWorkingPanel.GetActiveFrameModule().y;
        }

        private void toolStripButtonAddAnim_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
            object[] data = new object[dataGridViewAnim.ColumnCount];
            data[0] = dataGridViewAnim.Rows.Count;
            int id = Sprite.spriteInstance.AddAnim();
            data[1] = id.ToString("X");
            data[2] = false;
            data[3] = "";
            row.CreateCells(dataGridViewAnim, data);
            dataGridViewAnim.Rows.Add(row);
            dataGridViewAnim.ClearSelection();
            row.Selected = true;
        }

        private void toolStripButtonAddAnimFrame_Click(object sender, EventArgs e)
        {
            if (dataGridViewAnim.SelectedRows.Count == 0)
            {
                MessageBox.Show("Please select a animation");
                return;
            }
            if (Sprite.spriteInstance.frameList.Count() == 0)
            {
                MessageBox.Show("Please add frame first");
                return;
            }
            DataGridViewRow row = new DataGridViewRow();
            object[] data = new object[dataGridViewAnimFrame.ColumnCount];
            data[0] = dataGridViewAnimFrame.Rows.Count;
            int animID = Convert.ToInt32(dataGridViewAnim.SelectedRows[0].Cells["ColumnAnimID"].Value);
            data[1] = Sprite.spriteInstance.AddAnimFrame(animID).ToString("X"); ;
            data[2] = 100;
            row.CreateCells(dataGridViewAnimFrame, data);
            dataGridViewAnimFrame.Rows.Add(row);
            dataGridViewAnimFrame.ClearSelection();
            row.Selected = true;
            //frameWorkingPanel.Refresh();
        }

        private void toolStripButtonAnimPlayerPlay_Click(object sender, EventArgs e)
        {
            if (toolStripButtonAnimPlayerPlay.Image != imPause)
            {
                toolStripButtonAnimPlayerPlay.Image = imPause;
                animPlayerPanel.Play();
            }
            else
            {
                toolStripButtonAnimPlayerPlay.Image = imPlay;
                animPlayerPanel.Pause();
            }
        }

        private void dataGridViewAnim_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewAnim.SelectedRows.Count == 0) return;
            int id = Convert.ToInt32(dataGridViewAnim.SelectedRows[0].Cells["ColumnAnimID"].Value);
            animPlayerPanel.SetActiveAnim(Sprite.spriteInstance.GetAnim(id));
        }

        private void dataGridViewAnim_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            if (index < 0) return;
            int id = Convert.ToInt32(dataGridViewAnim.Rows[index].Cells["ColumnAnimID"].Value);
            bool loop = Convert.ToBoolean(dataGridViewAnim.Rows[index].Cells["ColumnAnimLoop"].Value);
            String d = Convert.ToString(dataGridViewAnim.Rows[index].Cells["ColumnAnimDesc"].Value);
            Sprite.spriteInstance.SetAnimValue(animPlayerPanel.GetActiveAnim().id, id, loop, d);
        }

        private void dataGridViewAnimFrame_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            if (index < 0) return;
            int id = Convert.ToInt32(dataGridViewAnimFrame.Rows[index].Cells["ColumnAnimFrameID"].Value);
            int time = Convert.ToInt32(dataGridViewAnimFrame.Rows[index].Cells["ColumnAnimFrameTime"].Value);
            Sprite.spriteInstance.SetAnimFrameValue(animPlayerPanel.GetActiveAnim().id, index, id, time);
            animPlayerPanel.Refresh();
        }

        private void dataGridViewAnimFrame_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewAnimFrame.SelectedRows.Count == 0) return;
            int index = Convert.ToInt32(dataGridViewAnimFrame.SelectedRows[0].Cells["ColumnAnimFrameID"].Value);
            animPlayerPanel.SetActiveAnimFrame(animPlayerPanel.GetActiveAnim().animFrameList[index]);
        }
        protected void InitUIFromSprite(Sprite sprite)
        {
            moduleWorkingPanel.SetActiveModule(null);
            frameWorkingPanel.SetActiveFrameModule(null);
            frameWorkingPanel.SetActiveFrame(null);
            animPlayerPanel.SetActiveAnimFrame(null);
            animPlayerPanel.SetActiveAnim(null);
            dataGridViewModule.Rows.Clear();
            dataGridViewFrame.Rows.Clear();
            dataGridViewFrameModule.Rows.Clear();
            dataGridViewAnim.Rows.Clear();
            dataGridViewAnimFrame.Rows.Clear();
            if (File.Exists(spritePath + sprite.imageName))
            {
                LoadImage(spritePath + sprite.imageName);
            }
            else
            {
                MessageBox.Show(sprite.imageName + " not found");
            }
            foreach (Module module in sprite.moduleList)
            {
                DataGridViewRow row = new DataGridViewRow();
                object[] data = new object[dataGridViewModule.ColumnCount];
                data[0] = dataGridViewModule.Rows.Count;
                data[1] = module.id.ToString("X");
                data[2] = module.x;
                data[3] = module.y;
                data[4] = module.width;
                data[5] = module.height;
                data[6] = module.description;
                row.CreateCells(dataGridViewModule, data);
                dataGridViewModule.Rows.Add(row);
            }
            foreach (Frame frame in sprite.frameList)
            {
                DataGridViewRow row = new DataGridViewRow();
                object[] data = new object[dataGridViewFrame.ColumnCount];
                data[0] = dataGridViewFrame.Rows.Count;
                data[1] = frame.id.ToString("X");
                data[2] = frame.description;
                row.CreateCells(dataGridViewFrame, data);
                dataGridViewFrame.Rows.Add(row);
            }
            foreach (Anim anim in sprite.animList)
            {
                DataGridViewRow row = new DataGridViewRow();
                object[] data = new object[dataGridViewAnim.ColumnCount];
                data[0] = dataGridViewAnim.Rows.Count;
                data[1] = anim.id.ToString("X");
                data[2] = anim.loop;
                data[3] = anim.description;
                row.CreateCells(dataGridViewAnim, data);
                dataGridViewAnim.Rows.Add(row);
            }
        }

        private void toolStripButtonAddBoundingGroup_Click(object sender, EventArgs e)
        {
            if (boundWorkingPanel.GetActiveFrame() == null) return;
            TextInput dialog = new TextInput("New bounding group", "Please type a name for bounding group");
            dialog.ShowDialog();
            if (dialog.Value != "")
            {
                int id = boundWorkingPanel.GetActiveFrame().AddBoundingGroup(dialog.Value);
                treeViewBounding.Nodes.Add(id.ToString() + ": " + dialog.Value);
            }
        }

        private void tabPageBounding_Enter(object sender, EventArgs e)
        {
            dataGridViewBoundFrame.Rows.Clear();
            foreach (Frame frame in Sprite.spriteInstance.frameList)
            {
                DataGridViewRow row = new DataGridViewRow();
                object[] data = new object[dataGridViewBoundFrame.ColumnCount];
                data[0] = dataGridViewBoundFrame.Rows.Count;
                data[1] = frame.id.ToString("X");
                data[2] = frame.description;
                row.CreateCells(dataGridViewBoundFrame, data);
                dataGridViewBoundFrame.Rows.Add(row);
            }
        }

        private void dataGridViewBoundFrame_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewBoundFrame.SelectedRows.Count == 0) return;
            int id = Convert.ToInt32(dataGridViewBoundFrame.SelectedRows[0].Cells["ColumnBoundID"].Value);
            if (Sprite.spriteInstance.GetFrame(id) != boundWorkingPanel.GetActiveFrame())
            {
                boundWorkingPanel.SetActiveFrame(Sprite.spriteInstance.GetFrame(id));
                treeViewBounding.Nodes.Clear();
                foreach (BoundGroup bg in boundWorkingPanel.GetActiveFrame().boundGroups)
                {
                    TreeNode node = new TreeNode(bg.id.ToString() + ": " + bg.description);
                    foreach (Bound bound in bg.bounds)
                    {
                        switch (bound.GetType())
                        {
                            case Bound.BoundType.Circle:
                                node.Nodes.Add("Circle: " + ((BoundCircle)bound).x + " " + ((BoundCircle)bound).y + " " + ((BoundCircle)bound).r);
                                break;
                            case Bound.BoundType.Rectangle:
                                node.Nodes.Add("Rectangle: " + ((BoundRectangle)bound).x + " " + ((BoundRectangle)bound).y + " " + ((BoundRectangle)bound).w + " " + ((BoundRectangle)bound).h);
                                break;
                            case Bound.BoundType.Unknown:
                                node.Nodes.Add("Unknown:");
                                break;
                        }
                    }
                    treeViewBounding.Nodes.Add(node);
                }
            }
        }

        private void toolStripButtonAddRectBound_Click(object sender, EventArgs e)
        {
            if (boundWorkingPanel.GetActiveBoundGroup() == null) return;
            BoundRectangle br = new BoundRectangle();
            boundWorkingPanel.GetActiveBoundGroup().bounds.Add(br);
            TreeNode node = treeViewBounding.SelectedNode;
            while (node.Level > 0)
            {
                node = node.Parent;
            }
            node.Nodes.Add("Rectangle: " + br.x + " " + br.y + " " + br.w + " " + br.h);
            boundWorkingPanel.Invalidate();
        }

        private void treeViewBounding_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                String[] s = e.Node.Text.Split(':');
                int id = Convert.ToInt32(s[0]);
                boundWorkingPanel.SetActiveBoundGroup(boundWorkingPanel.GetActiveFrame().GetBoundGroup(id));
            }
            else if (e.Node.Level == 1)
            {
                String[] s = e.Node.Parent.Text.Split(':');
                int id = Convert.ToInt32(s[0]);
                boundWorkingPanel.SetActiveBoundGroup(boundWorkingPanel.GetActiveFrame().GetBoundGroup(id));
                int index = e.Node.Index;
                boundWorkingPanel.SetActiveBound(boundWorkingPanel.GetActiveBoundGroup().bounds[index]);
            }
        }
        public void UpdateActiveBound()
        {
            if (boundWorkingPanel.GetActiveBound() != null && treeViewBounding.SelectedNode != null && treeViewBounding.SelectedNode.Level > 0)
            {
                treeViewBounding.SelectedNode.Text = boundWorkingPanel.GetActiveBound().GetString();
            }
        }

        private void treeViewBounding_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right && e.Node.Level == 0)
            {
                TextInput dialog = new TextInput("Rename", "Please type a new name");
                dialog.Value = boundWorkingPanel.GetActiveBoundGroup().description;
                dialog.ShowDialog();
                if (dialog.Value != "")
                {
                    boundWorkingPanel.GetActiveBoundGroup().description = dialog.Value;
                    e.Node.Text = boundWorkingPanel.GetActiveBoundGroup().id.ToString() + ": " + dialog.Value;
                }
            }
        }

        private void treeViewBounding_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (treeViewBounding.SelectedNode.Level == 1)
                {
                    boundWorkingPanel.GetActiveBoundGroup().bounds.Remove(boundWorkingPanel.GetActiveBound());
                    boundWorkingPanel.SetActiveBound(null);
                    treeViewBounding.Nodes.Remove(treeViewBounding.SelectedNode);
                }
                else if (treeViewBounding.SelectedNode.Level == 0)
                {
                    boundWorkingPanel.GetActiveFrame().boundGroups.Remove(boundWorkingPanel.GetActiveBoundGroup());
                    boundWorkingPanel.SetActiveBound(null);
                    boundWorkingPanel.SetActiveBoundGroup(null);
                    treeViewBounding.Nodes.Remove(treeViewBounding.SelectedNode);
                }
            }
        }

        private void toolStripButtonAddCircleBound_Click(object sender, EventArgs e)
        {
            if (boundWorkingPanel.GetActiveBoundGroup() == null) return;
            BoundCircle br = new BoundCircle();
            boundWorkingPanel.GetActiveBoundGroup().bounds.Add(br);
            TreeNode node = treeViewBounding.SelectedNode;
            while (node.Level > 0)
            {
                node = node.Parent;
            }
            node.Nodes.Add("Circle: " + br.x + " " + br.y + " " + br.r);
            boundWorkingPanel.Invalidate();
        }

        private void treeViewBounding_KeyDown(object sender, KeyEventArgs e)
        {
            boundWorkingPanel.KeyProcess(e.KeyCode);
            e.Handled = true;
        }
    }
}
